package com.zzzy.doc.service.mydoc;

import com.zzzy.common.util.AssertUtil;
import com.zzzy.doc.common.enums.DocStatusEnum;
import com.zzzy.doc.dao.entity.DocFile;
import com.zzzy.doc.dao.entity.Document;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class MyDocRedSheetService extends MyDocService {

    public void updateRedSheetFile(Long id, byte[] fileBytes) {
        Document document = getDocByIdForUpdate(id);
        docFileService.updateRedSheetFile(id, fileBytes);
        document.setUpdateTime(LocalDateTime.now());
        baseMapper.updateById(document);
    }

    @Transactional
    public void redSheetCompleteWithSavePdf(Long id, byte[] fileBytes) {
        Document document = getDocByIdForUpdate(id);
        docAdviceService.addAdvice(id, "清稿套红完毕进入签章阶段");

        DocFile docFile = docFileService.getDocFileByDocId(id);
        AssertUtil.isTrue(docFile.getMainRevisionCount() == 0, "正文存在修订痕迹或批注，无法进入签章阶段");
        AssertUtil.isEquals(DocStatusEnum.RED_SHEET_TEMPLATE.getValue(), document.getStatus(), "公文状态错误，无法进入签章阶段");
        document.setStatus(DocStatusEnum.SIGN.getValue());
        document.setUpdateTime(LocalDateTime.now());
        baseMapper.updateById(document);

        docFileService.updateFinalPdfByDocId(id, fileBytes);
    }
}
